<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/20
 * Time: 下午5:44
 */

class ModShop {
	
	public static function oneData($params){
		$table = 'tblgameshoponestatis';
		$where  = ' WHERE RecDate BETWEEN :begin AND :end
        		       AND Period=:period
                       AND GameSrc=:game ';
	
		$args   = array(
				':begin'    => $params['time_start'],
				':end'      => $params['time_end'],
				':period'   => 'Daily',
				':game'     => $params['g'],
		);
		
		if(!empty($params['currency_type'])){
			$where .= " AND CurrencyType = :currency_type ";
			$args['currency_type'] = $params['currency_type'];
		}
	
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
	
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
	
		$sql = "SELECT GoodsId,GoodsPrice,CurrencyType,
		SUM(PayMoney) AS PayMoney, SUM(PayUser) AS PayUser,
		SUM(PayCount) AS PayCount
		FROM {$table} {$where}
		GROUP BY CurrencyType,GoodsId,GoodsPrice ";
	
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
		return $dbLink->query($sql, $args);
	}
	
	
	public static function allData($params){
		$table = 'tblgameshopallstatis';
		$where  = ' WHERE RecDate BETWEEN :begin AND :end
        		       AND Period=:period
                       AND GameSrc=:game ';
	
		$args   = array(
				':begin'    => $params['time_start'],
				':end'      => $params['time_end'],
				':period'   => 'Daily',
				':game'     => $params['g'],
		);
		
		if(!empty($params['currency_type'])){
			$where .= " AND CurrencyType = :currency_type ";
			$args['currency_type'] = $params['currency_type'];
		}
	
		$user = AccAuth::me();
		if($params['os'] != 'ALL' || $user['IsViewAll'] != 1){
			$where .= " AND Platform=:p ";
			$args[':p'] = $params['os'];
		}
	
		if($params['os'] == 'ALL' && $params['server'] == 'ALL'){  //都为all，查询专服下的所有渠道的服务器
			$modServer = new ModServer();
			$list = $modServer->getServerForSpecialId($params['g'],$params['special_id']);
			$where .= " AND ( ";
			foreach ($list as $key=>$val){
				$where .= " SID = '{$val['develop_id']}'  or";
			}
			$where = substr($where, 0, -2);
			$where .= " ) ";
		}else{
			$where .= " AND SID=:ser ";
			$args[':ser'] = empty($params['server']) ? 'ALL' : $params['server'];
		}
	
		$sql = "SELECT CurrencyType,
		SUM(PayMoney) AS PayMoney, SUM(PayUser) AS PayUser,
		SUM(PayCount) AS PayCount
		FROM {$table} {$where}
		GROUP BY CurrencyType ";
	
		$dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
		return $dbLink->query($sql, $args);
	}

    public static function indexData($params){
        $table = 'tblGameShop';
        $where  = ' WHERE RecDate = :date AND Platform=:platform AND GameSrc=:game AND CoinType = :coinType ';
        $args   = array(
            ':date'     => $params['date'],
            ':period'   => $params['period'],
            ':game'     => $params['g'],
            ':platform' => $params['os'],
            ':coinType' => $params['coinType'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else{
            $where .= 'AND SID = "ALL"';
        }


        $sql = "SELECT * FROM {$table} {$where} order by RecDate desc, SID asc";
        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'],$params['g']));
        return $dbLink->query($sql, $args);
    }

    public static function blackData($params){
        $table = 'tblGameBlackMark';
        $where  = ' WHERE RecDate = :date AND Platform=:platform AND GameSrc=:game AND CoinType = :coinType ';
        $args   = array(
            ':date'     => $params['date'],
            ':period'   => $params['period'],
            ':game'     => $params['g'],
            ':platform' => $params['os'],
            ':coinType' => $params['coinType'],
        );

        if(!empty($params['server']) && $params['server'] !== 'ALL'){
            $where .= 'AND SID=:server';
            $args[':server'] = $params['server'];
        }else{
            $where .= 'AND SID = "ALL"';
        }


        $sql = "SELECT * FROM {$table} {$where} order by RecDate desc, SID asc";
        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'],$params['g']));
        return $dbLink->query($sql, $args);
    }

    public static function analysisData($params){
        $where = ' WHERE RecDate BETWEEN :t_s AND :t_e
                        AND Period=:period
                        AND Platform=:p
                        AND GameSrc=:g
                        AND SID=:ser
                        AND CoinType=1';

        $args = array(
            ':t_s'    => $params['time_start'],
            ':t_e'    => $params['time_end'],
            ':g'      => empty($params['g']) ? 'ALL' : $params['g'],
            ':p'      => $params['os'],
            ':ser'    => empty($params['server']) ? 'ALL' : $params['server'],
            ':period' => 'Daily'
        );

        $table = 'tblGameShop';

        $sql = "SELECT * FROM {$table} {$where}";

        $dbLink = new PubDbLink('dbSelf', PubDatabase::getMgmtDbName($params['p'], $params['g']));
        $res = $dbLink->query($sql, $args);
        return $res;
    }

} 
